<!--
 * @Description: 
 * @Author: wuwl
 * @Date: 2023-10-26 15:14:28
 * @FilePath: \web-case-exercise\js\MessageChannel\4.输出顺序.html
 * @LastEditTime: 2024-04-11 15:47:46
 * @LastEditors: wuwl
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>

        setTimeout(() => {
            console.log('setTimeout')
        }, 0)

        const { port1, port2 } = new MessageChannel()
        port2.onmessage = function () {
            console.log('MessageChannel')
        }
        port1.postMessage('ping')

        requestAnimationFrame(() => {
            console.log('requestAnimationFrame')
        })

        Promise.resolve().then(() => {
            console.log('Promise')
        })

        // 结果
        //         Promise
        // 4.输出顺序.html:25 requestAnimationFrame
        // 4.输出顺序.html:15 setTimeout
        // 4.输出顺序.html:20 MessageChannel

        // 1. requestAnimationFrame打印时机不稳定，因为不是每次事件循环都会触发重渲染，浏览器可能将多次渲染合成一次；
        // 2. 在旧版本chrome上MessageChannel会先于setTimeout打印，在新版本chrome上则反过来，应该是chrome在某个版本上修改了宏任务优先级的实现。
    </script>
</body>

</html>